package com.example.demoserver.controller;

import com.example.demoserver.entity.*;
import com.example.demoserver.mqtt.TestMqttHandler;
import com.example.demoserver.mqtt.TestServer;
import com.example.demoserver.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/mqtt")
@RequiredArgsConstructor
@Component
@Slf4j
public class MqttController {
    private final InteDatapointService inteDatapointService;
    private final InteGatewayService inteGatewayService;
    private final InteAppstateService inteAppstateService;
    private final InteApplogService inteApplogService;
    private final InteAppalarmService inteAppalarmService;
    private final InteGatewaylogService inteGatewaylogService;

    @GetMapping("/startmqtt")
    public boolean startMqtt() {
        TestServer testServer = new TestServer(inteDatapointService,inteGatewayService,inteAppstateService,inteAppalarmService,inteGatewaylogService);
        boolean bol=testServer.run();
        if(bol){
            InteApplog inteApplog=insertLog(0);
            inteApplogService.save(inteApplog);
        }
        return bol;
    }
    @GetMapping("/stopmqtt")
    public boolean stopMqtt() throws InterruptedException {
        TestServer testServer = new TestServer(inteDatapointService,inteGatewayService,inteAppstateService,inteAppalarmService,inteGatewaylogService);
        boolean bol=testServer.stop();
        if(bol){
            TestMqttHandler testMqttHandler=new TestMqttHandler(inteDatapointService,inteGatewayService,inteAppstateService,inteAppalarmService,inteGatewaylogService);
            InteApplog inteApplog=insertLog(1);
            inteApplogService.save(inteApplog);
            for(int i=0;i<testMqttHandler.conList.size();i++){
                InteGateway inteGateway = inteGatewayService.selectByGIdAndPas(testMqttHandler.conList.get(i).get(1), testMqttHandler.conList.get(i).get(2));
                inteGateway.setGatewaystate(1);
                inteGatewayService.updateById(inteGateway);
                testMqttHandler.conList.remove(i);
                saveLog("离线","无","设备"+inteGateway.getGatewayname()+"离线");
            }
            return true;
        }
        return false;
    }
    public InteApplog insertLog(Integer state){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatted = now.format(formatter);
        InteApplog inteApplog=new InteApplog();
        inteApplog.setAppname("设备管理");
        inteApplog.setAppstate(state);
        inteApplog.setOptime(formatted);
        return inteApplog;
    }
    public void saveLog(String op,String type,String value){
        InteGatewaylog inteGatewaylog=new InteGatewaylog();
        inteGatewaylog.setHtype(type);
        inteGatewaylog.setLogoperate(op);
        inteGatewaylog.setLogvalue(value);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatted = now.format(formatter);
        inteGatewaylog.setLogtime(formatted);
        inteGatewaylogService.save(inteGatewaylog);
    }
}

